﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace PCRemoteMobileVer
{
    class Cursor
    {
        public enum Statement
        {
            CLICK, DOUBLECLICK, DRAG, RELEASE
        }
        public Vector2 Position;
        private Texture2D _texture2D;
        public bool IsBusy = false;
        private Vector2 beginDrag;
        private Vector2 endDrag;
        private Vector2 beginTouch;
        public Statement state;
        public Cursor(ContentManager content)
        {
            _texture2D = content.Load<Texture2D>("cursor");
            Position = new Vector2(400, 240);
            state = Statement.RELEASE;
        }

        public void Update()
        {
            //Get Input
            TouchCollection touchLocations = TouchPanel.GetState();
            foreach (var touchLocation in touchLocations)
            {
                if (touchLocation.State == TouchLocationState.Pressed)
                {
                    IsBusy = true;
                    beginDrag = touchLocation.Position;
                    beginTouch = touchLocation.Position;
                }
                else if (touchLocation.State == TouchLocationState.Moved)
                {
                    endDrag = touchLocation.Position;
                    Vector2 deltaDrag = endDrag - beginDrag;
                    Position += deltaDrag;
                    beginDrag = touchLocation.Position;
                    state = Statement.DRAG;
                }
                else
                {
                    state = Statement.RELEASE;
                    IsBusy = false;
                    Vector2 deltaTouch = touchLocation.Position - beginTouch;
                    if (Math.Abs(deltaTouch.X) < 5f && Math.Abs(deltaTouch.Y) < 5f)
                    {
                        state = Statement.CLICK;
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_texture2D, Position, Color.White);
        }
    }
}
